<?php
class Design extends AppModel {
	var $name = 'Design';
	var $validate = array(
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'pitch_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'title' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Pitch' => array(
			'className' => 'Pitch',
			'foreignKey' => 'pitch_id',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'counterCache' => true,
		)
	);

	var $hasMany = array(
		'Comment' => array(
			'className' => 'Comment',
			'foreignKey' => 'design_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'DesignPhoto' => array(
			'className' => 'DesignPhoto',
			'foreignKey' => 'design_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	
	var $actsAs = array('Containable');
	
	function get_designs($id = null){
		if ($id == null) {
			return false;
		} else {
			$designs = $this->find('all', array('conditions' => array('Design.pitch_id' => $id), 'contain' => array('DesignPhoto', 'Pitch')));
			return $designs;
		}
	}
	
	function get_design($design_id = null) {
		if ($design_id == null) {
			return false;
		} else {
			$conditions = array(
				'Design.id' => $design_id,
			);
			$contain = array('Pitch');
			$design = $this->find('first', array('conditions' => $conditions, 'contain' => $contain));
			if ($design['Design']['user_id'] == $_SESSION['Auth']['User']['id'] or $design['Pitch']['user_id'] == $_SESSION['Auth']['User']['id']) {
				$contain = array(
					'Pitch',
					'User',
					'DesignPhoto',
					'Comment' => array(
						'User' => array(
							'ContractorProfile',
							'DesignerProfile',
						)
					)
				);
				$design = $this->find('first', array('contain' => $contain, 'conditions' => $conditions));
				$design['showComments'] = true;
			} else {
				$contain = array(
					'DesignPhoto',
					'Pitch',					
					'User' => array(
						'ContractorProfile',
						'DesignerProfile',
					)
				);
				$design = $this->find('first', array('contain' => $contain, 'conditions' => $conditions));
				$design['showComments'] = false;
			}
		}
		return $design;
	}

	function get_updates() {
		if ($_SESSION['Auth']['User']['group_id'] == 2) {
			$conditions = array(
				'Pitch.user_id' => $_SESSION['Auth']['User']['id'],
			);
		} else {
			$conditions = array(
				'Pitch.id' => array(
					'PitchSubscription.user_id' => $_SESSION['Auth']['User']['id']
				),
				'NOT' => array(
					'Design.user_id' => $_SESSION['Auth']['User']['id']
				)
			);
		}
		$designs = $this->find('all', array('conditions' => $conditions, 'order' => array('Design.created' => 'DESC'), 'limit' => 10));
		return $designs;
	}

}
?>
